<?php
/**
 * News controller for News Component
 * @package Joomlart
 * @subpackage Component
 */
//Check to ensure this file is included in Joomla
defined('_JEXEC') or die( 'Restricted access' );

jimport('joomla.application.component.controller');
/**
 * News controler
 */
class JanewsControllerlayouttemplates extends JController
{
	/**
	 * Custom Constructor
	 */
	function __construct( $default = array())
	{
		parent::__construct( $default );

		$this->registerTask( 'apply',	'save');
		$this->registerTask( 'edit', 'display');
		$this->registerTask( 'add', 'display');
		$this->registerTask( 'next', 'display');
	}
	
	function display( )
	{
		switch($this->getTask())
		{
			case 'edit'    :
			{
				JRequest::setVar( 'hidemainmenu', 1 );
				JRequest::setVar( 'layout', 'form'  );
				JRequest::setVar( 'view', 'layouttemplates'  );
				JRequest::setVar( 'edit', true );
			} break;

			case 'add' :
			{
				JRequest::setVar( 'hidemainmenu', 1 );
				JRequest::setVar( 'layout', 'form'  );
				JRequest::setVar( 'view', 'layouttemplates'  );
				JRequest::setVar( 'edit', false );
			} break;
			
			case 'next' :
		  { 
      	JRequest::setVar( 'hidemainmenu', 1 );
				JRequest::setVar( 'layout', 'form'  );
				JRequest::setVar( 'view', 'layouttemplates'  );
				JRequest::setVar( 'edit', true );	    
		  }
		}

		//Set the default view, just in case
		$view = JRequest::getCmd('view');
		if(empty($view)) {
			JRequest::setVar('view', 'layouttemplates');
			JRequest::setVar('layout', 'default');
		}
		
		parent::display();
	}
	
	function save()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$db	= & JFactory::getDBO();

		// save the layout template parent information
		$post	= JRequest::get( 'post' );
		
		$row = JTable::getInstance( 'janews_templates', 'Table' );
		
		$obj = new stdClass();
		$obj->title = htmlspecialchars($post['title'], ENT_QUOTES, 'UTF-8');
		$obj->type = $post['tmpl_type'];
		$obj->template = $post['template'];
		$id = $post['cid'];
		$isNew = ($id == 0);
		
		// Get parameter variables from the request
		$params		= JRequest::getVar( 'params', null, 'post', 'array' );

		// Build parameter INI string
		if (is_array($params))
		{
			$txt = array ();
			foreach ($params as $k => $v) {
				$txt[] = "$k=$v";
			}
			$obj->params = implode("\n", $txt);
		}
		
		if ($isNew) {
			$db->insertObject('#__janews_templates', $obj);
			$query = 'SELECT MAX(id) FROM #__janews_templates';
			$db->setQuery($query);
			$id = $db->loadResult();
			$msg = JText::_( 'LAYOUT TEMPLATE CREATED' );
		}
		else {
			$obj->id = $id;
			$db->updateObject('#__janews_templates', $obj, 'id');
			$msg = JText::_( 'CHANGES TO LAYOUT TEMPLATE SAVED' );
		}
		$link = 'index.php?option=com_janews&controller=layouttemplates&group='.$post['tmpl_type'];
		if ($this->_task=='apply') {
		  $link = 'index.php?option=com_janews&controller=layouttemplates&task=edit&cid[]='.$id;
		}
		
		$this->setRedirect($link, $msg);
	}

	function remove()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$db		=& JFactory::getDBO();
		$cid	= JRequest::getVar( 'cid', array(), '', 'array' );
    $count = 0;
		JArrayHelper::toInteger($cid);
		$msg = '';

		for ($i=0, $n=count($cid); $i < $n; $i++)
		{
			$tmpl =& JTable::getInstance('janews_templates', 'Table');
			if (!$tmpl->delete( $cid[$i] ))
				$msg .= $tmpl->getError();
			else
			  $count++;
		}
		$msg = JText::sprintf( $count.' Item(s) deleted');
		$this->setRedirect( 'index.php?option=com_janews&controller=layouttemplates', $msg );
	}
	
	/**
	 * cancel editing a record
	 * @return void
	 */
	function cancel()
	{
	  $group = JRequest::getVar('group');
		$msg = JText::_( 'OPERATION CANCELLED' );
		$this->setRedirect( 'index.php?option=com_janews&controller=layouttemplates&group='.$group, $msg );
	}
}
?>